package com.example.demo1.Controller;

import com.example.demo1.Bean.UserBean;
import com.example.demo1.Service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.SerializableString;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminUserContro {
    @Resource
    UserService userservice;
    List<UserBean> userList = new ArrayList<>();
    PageInfo pageInfo = new PageInfo();
    int pageSize = 10;
    String kw ="";
    int x = 0;

    @RequestMapping("/user")
    String showAllUser(Model model,String pageNum,String isLogin,String UserId,String state,String UserName,String UserSex,String UserPhone,String UserAddress,String LastDateArry) throws JsonProcessingException {

        if(LastDateArry==null)
            LastDateArry="1";
        System.out.println(LastDateArry);
        if (pageNum==null|| Objects.equals(pageNum, "0"))
            pageNum="1";
        if(isLogin!=null)
           userservice.updateUser(isLogin,UserId,UserName,UserSex,UserPhone,UserAddress);
        if(state==null)
            state= String.valueOf(0);
        System.out.println("----------");
        System.out.println(state);
//        System.out.println(pageNum);
//        System.out.println(isLogin);
//        System.out.println(UserId);
//        System.out.println(state);

        pageInfo = userservice.selectAllUser(Integer.parseInt(pageNum), pageSize, LastDateArry);
        if(state.equals("1")){
            kw=null;
        }
        if(kw!=null)
                pageInfo = userservice.selectUserBykw(Integer.parseInt(pageNum), pageSize, kw, LastDateArry);

        List<UserBean> userList1 = userservice.getAllUser();

        userList = pageInfo.getList();
        System.out.println(userList);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("userList",userList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("LastDateArr", LastDateArry);
        model.addAttribute("state", 0);
        model.addAttribute("keyword",kw);
        model.addAttribute("userid",userList1.get(userList1.size()-1).getUserId());
        return "account";
    }

    @RequestMapping("/searchuser")
    String searchUser(Model model,String pageNum,String isLogin,String UserId,String state,String keyword,String UserName,String UserSex,String UserPhone,String UserAddress,String LastDateArrorder) throws JsonProcessingException {
        kw=keyword;
        if(LastDateArrorder==null)
            LastDateArrorder="1";
        if (pageNum==null|| Objects.equals(pageNum, ""))
            pageNum="1";
        if(isLogin!=null)
            userservice.updateUser(isLogin,UserId,UserName,UserSex,UserPhone,UserAddress);
        System.out.println(isLogin);
        System.out.println(UserId);
        PageInfo pageInfo = userservice.selectUserBykw(Integer.parseInt(pageNum), pageSize,kw,LastDateArrorder);
        userList = pageInfo.getList();
        System.out.println(userList);
        List<UserBean> userList1 = userservice.getAllUser();
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("userList",userList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("LastDateArr", 1);
        model.addAttribute("keyword",kw);
        model.addAttribute("userid",userList1.get(userList1.size()-1).getUserId());
        return "account";
    }


    @RequestMapping("/deleteUser")
    String deleteUser(Model model, String UserId) {
        if (!UserId.equals("1")) {
            userservice.deleteUser(UserId);
        }
        PageInfo pageInfo = userservice.selectAllUser(1, pageSize,"1");
        userList = pageInfo.getList();
        System.out.println(userList);
        List<UserBean> userList1 = userservice.getAllUser();
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("userList",userList);
        model.addAttribute("pageNum", 1);
        model.addAttribute("LastDateArr", 1);
        model.addAttribute("userid",userList1.get(userList1.size()-1).getUserId());
        return "account";
    }








}
